package cn.hotel.mapper;

import cn.hotel.pojo.HouseType;
import cn.hotel.utlis.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HouseTypeDaoImpl extends BaseDao implements HouseTypeDao{
    @Override
    public List<HouseType> getHouseTypeList(String houseType, int index, int size) {
        String sql = "SELECT * FROM HOUSE_TYPE ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> param = new ArrayList<>();
        if (StringUtil.isNotEmpty(houseType)) {
            sbf.append(" WHERE HOUSETYPE = ?");
            param.add(houseType);
        }
        sbf.append(" LIMIT ?,?");
        param.add(index);
        param.add(size);
        ResultSet rs = this.executeQuery(sbf.toString(),param);
        List<HouseType> houseTypeList = new ArrayList<>();
        HouseType houseType1 = null;
        try {
            while (rs.next()){
                houseType1 = new HouseType();
                houseType1.setId(rs.getLong(1));
                houseType1.setHouseType(rs.getString(2));
                houseType1.setPrice(rs.getString(3));
                houseType1.setParticulars(rs.getString(4));
                houseType1.setHouseNum(rs.getInt(5));


                houseTypeList.add(houseType1);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeSource(rs);
        }
        return houseTypeList;
    }

    @Override
    public int getCount(String houseType) {
        String sql = "SELECT COUNT(1) FROM HOUSE_TYPE ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> param = new ArrayList<>();
        if (StringUtil.isNotEmpty(houseType)) {
            sbf.append(" WHERE HOUSETYPE = ?");
            param.add(houseType);
        }
        int count = 0;
        ResultSet rs = this.executeQuery(sbf.toString(),param);
        try {
            while (rs.next()){
                count = rs.getInt(1);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeSource(rs);
        }
        return count;

    }

    @Override
    public int addHouseType(HouseType houseType) {
        String sql = "INSERT INTO `HOUSE_TYPE`(HOUSETYPE,PRICE,PARTICULARS,HOUSENUM) VALUES (?,?,?,?)";
        List<Object> param = new ArrayList<>();
        param.add(houseType.getHouseType());
        param.add(houseType.getPrice());
        param.add(houseType.getParticulars());
        param.add(houseType.getHouseNum());

        return this.executeUpdate(sql,param);
    }

    @Override
    public int deleteHouseType(String houseType) {
        String sql = "DELETE FROM HOUSE_TYPE WHERE HOUSETYPE = ?";
        List<Object> param = new ArrayList<>();
        param.add(houseType);
        int resultSet = this.executeUpdate(sql,param);
        return resultSet;

    }

    @Override
    public int revampHouseType(HouseType houseType) {
        String sql = "UPDATE HOUSE_TYPE SET HOUSETYPE = ?,PRICE = ?,PARTICULARS = ?,HOUSENUM = ? WHERE HOUSETYPE = ?";
        List<Object> param = new ArrayList<>();
        param.add(houseType.getHouseType());
        param.add(houseType.getPrice());
        param.add(houseType.getParticulars());
        param.add(houseType.getHouseNum());
        param.add(houseType.getHouseType());
        int resultSet = this.executeUpdate(sql,param);
        return resultSet;
    }
}
